home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / MSG Demo 1.2 Source / MSG Demo ƒ / Demo code / demo crash.c next >
Encoding:
C/C++ Source or Header  |  1993-11-10  |  11.0 KB  |  562 lines  |  [TEXT/KAHL]

  1. /**********************************************************************\
  2.  
  3. File:        demo crash.c
  4.  
  5. Purpose:    This module handles crashing your machine with grace and
  6.             style.
  7.  
  8.  
  9. MSG Demo -- graphic effects demonstration program
  10. Copyright (C) 1992-3 Mark Pilgrim & Dave Blumenthal
  11.  
  12. This program is free software; you can redistribute it and/or modify
  13. it under the terms of the GNU General Public License as published by
  14. the Free Software Foundation; either version 2 of the License, or
  15. (at your option) any later version.
  16.  
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. GNU General Public License for more details.
  21.  
  22. You should have received a copy of the GNU General Public License
  23. along with this program in a file named "GNU General Public License".
  24. If not, write to the Free Software Foundation, 675 Mass Ave,
  25. Cambridge, MA 02139, USA.
  26.  
  27. \**********************************************************************/
  28.  
  29. #include "demo crash.h"
  30. #include "msg sounds.h"
  31. #include "msg environment.h"
  32. #include "msg timing.h"
  33.  
  34. #define BOXSIZE 20
  35. #define CorrectTime 1
  36.  
  37. void RestoreScreen(int oldMenuHeight)
  38. {
  39.     TextFont(0);
  40.     TextSize(0);
  41.     TextMode(srcOr);
  42.     FlushEvents(mDownMask+mUpMask+keyDownMask+keyUpMask+autoKeyMask, 0L);
  43.     MBarHeight=oldMenuHeight;
  44.     DrawMenuBar();
  45.     PaintOne(0L,GrayRgn);
  46.     PaintBehind(WindowList,GrayRgn);
  47.     ShowCursor();
  48.     ObscureCursor();
  49. }
  50.  
  51. void CrashAndBurn(int whichSystemError)
  52. {
  53.     int                oldMenuHeight;
  54.     int                whichWipe;
  55.     
  56.     DoSound(sound_dontpanic, FALSE);
  57.     
  58.     ShutDownEnvironment();        /* If you have to crash a computer, */
  59.                                 /* it costs nothing to be polite. */
  60.     SysError(whichSystemError);
  61.     
  62.     /* in case the user manages to continue, we’ll do neat stuff here */
  63.     HideCursor();
  64.     oldMenuHeight=MBarHeight;
  65.     MBarHeight=0;
  66.     DrawMenuBar();
  67.     SetPort(WMgrPort);
  68.     whichWipe=(TickCount()&0x7fffffff)%6;
  69.     switch (whichWipe)
  70.     {
  71.         case 0:    CrashSkipaline(WMgrPort);    break;
  72.         case 1:    CrashBoxIn(WMgrPort);        break;
  73.         case 2:    CrashScroll(WMgrPort);        break;
  74.         case 3:    CrashMrDoOutdone(WMgrPort);    break;
  75.         case 4:    CrashMrDo(WMgrPort);        break;
  76.         case 5:    CrashSpiral(WMgrPort);        break;
  77.     }
  78.     
  79.     RestoreScreen(oldMenuHeight);
  80.     
  81.     gDone=TRUE;                    /* we can’t actually continue with the program, */
  82.                                 /* since, after all, the computer thinks we’ve crashed */
  83. }
  84.  
  85. void CrashSkipaline(GrafPtr thePtr)
  86. {
  87.     Rect        thisone,thatone;
  88.     int            width,height;
  89.  
  90.     SetPort(thePtr);
  91.     
  92.     width=thePtr->portRect.right-thePtr->portRect.left;
  93.     height=thePtr->portRect.bottom-thePtr->portRect.top;
  94.         
  95.     thisone.top=thisone.left=thatone.left=0;
  96.     thisone.bottom=1;
  97.     thatone.bottom=height;
  98.     thatone.top=height-1;
  99.     thisone.right=thatone.right=width;
  100.     
  101.     while (thisone.top<height)
  102.     {
  103.         StartTiming();
  104.         FillRect(&thisone, black);
  105.         FillRect(&thatone, black);
  106.         
  107.         thisone.top+=2;             /* even row goes down by 2 */
  108.         thisone.bottom+=2;
  109.         thatone.top-=2;             /* odd row goes up by 2 */
  110.         thatone.bottom-=2;
  111.         TimeCorrection(1);
  112.     }
  113. }
  114.  
  115. void CrashBoxIn(GrafPtr thePtr)
  116. {
  117.     Rect        vsource1,vsource2, hsource1, hsource2;
  118.     int            vbar,hbar;
  119.     int            width,height;
  120.     int            VBarGap, HBarGap;
  121.     
  122.     SetPort(thePtr);
  123.     
  124.     width=thePtr->portRect.right-thePtr->portRect.left;
  125.     height=thePtr->portRect.bottom-thePtr->portRect.top;
  126.     
  127.     VBarGap=10;
  128.     HBarGap=VBarGap*height/width;
  129.     
  130.     vbar=0;
  131.     hbar=0;
  132.     vsource1.top=vsource2.top=hsource2.left=hsource1.left=0;  /* these */
  133.     vsource1.bottom=vsource2.bottom=height;       /* never */
  134.     hsource1.right=hsource2.right=width;          /* change */
  135.     while (vbar<width/2+VBarGap)
  136.     {
  137.         StartTiming();
  138.         vsource1.left=vbar;
  139.         vsource1.right=vsource1.left+VBarGap;
  140.         vsource2.right=width-vbar;
  141.         vsource2.left=vsource2.right-VBarGap;
  142.         hsource1.top=hbar;
  143.         hsource1.bottom=hsource1.top+HBarGap;
  144.         hsource2.bottom=height-hbar;
  145.         hsource2.top=hsource2.bottom-HBarGap;
  146.  
  147.         FillRect(&vsource1, black);
  148.         FillRect(&hsource1, black);
  149.         FillRect(&vsource2, black);
  150.         FillRect(&hsource2, black);
  151.  
  152.         vbar+=VBarGap;
  153.         hbar+=HBarGap;
  154.         TimeCorrection(3);
  155.     }
  156. }
  157.  
  158. void CrashScroll(GrafPtr thePtr)
  159. {
  160.     int            x, y;
  161.     Rect        theRect, dest;
  162.     Rect        scrollsource, scrolldest;
  163.     int            width,height;
  164.     
  165.     SetPort(thePtr);
  166.     
  167.     width=thePtr->portRect.right-thePtr->portRect.left;
  168.     height=thePtr->portRect.bottom-thePtr->portRect.top;
  169.         
  170.     scrollsource=thePtr->portRect;
  171.     scrollsource.bottom-=10;              /* whole screen minus bottom strip */
  172.     scrolldest = scrollsource;
  173.     OffsetRect(&scrolldest, 0, 10);       /* whole screen shifted down 10 */
  174.     
  175.     dest = thePtr->portRect;
  176.     dest.bottom=10;                       /* top strip for new data */
  177.     
  178.     for(x = height - 10; x >= 0; x -= 10)
  179.     {
  180.         StartTiming();
  181.         CopyBits(&(thePtr->portBits), &(thePtr->portBits),
  182.                 &scrollsource, &scrolldest, 0, 0L);
  183.         FillRect(&dest, black);
  184.         
  185.         TimeCorrection(3);
  186.     }
  187. }
  188.  
  189. void CrashMrDoOutdone(GrafPtr thePtr)
  190. {
  191.     int            x, y;
  192.     int            vgap,hgap;
  193.     Rect        theRect, dest;
  194.     Rect        scrollsource, scrolldest;
  195.     Rect        bounds[25];
  196.     int            width,height;
  197.     
  198.     SetPort(thePtr);
  199.     
  200.     width=thePtr->portRect.right-thePtr->portRect.left;
  201.     height=thePtr->portRect.bottom-thePtr->portRect.top;
  202.         
  203.     vgap=height/5;
  204.     hgap=width/5;
  205.     
  206.     for (x=0; x<25; x++)
  207.     {
  208.         switch (x)
  209.         {
  210.             case 0:
  211.             case 1:
  212.             case 2:
  213.             case 3:
  214.             case 4:
  215.                 bounds[x].top=0;
  216.                 break;
  217.             case 15:
  218.             case 16:
  219.             case 17:
  220.             case 18:
  221.             case 5:
  222.                 bounds[x].top=vgap;
  223.                 break;
  224.             case 14:
  225.             case 23:
  226.             case 24:
  227.             case 19:
  228.             case 6:
  229.                 bounds[x].top=vgap*2;
  230.                 break;
  231.             case 13:
  232.             case 22:
  233.             case 21:
  234.             case 20:
  235.             case 7:
  236.                 bounds[x].top=vgap*3;
  237.                 break;
  238.             case 12:
  239.             case 11:
  240.             case 10:
  241.             case 9:
  242.             case 8:
  243.                 bounds[x].top=vgap*4;
  244.                 break;
  245.         }
  246.         switch (x)
  247.         {
  248.             case 0:
  249.             case 15:
  250.             case 14:
  251.             case 13:
  252.             case 12:
  253.                 bounds[x].left=0;
  254.                 break;
  255.             case 1:
  256.             case 16:
  257.             case 23:
  258.             case 22:
  259.             case 11:
  260.                 bounds[x].left=hgap;
  261.                 break;
  262.             case 2:
  263.             case 17:
  264.             case 24:
  265.             case 21:
  266.             case 10:
  267.                 bounds[x].left=hgap*2;
  268.                 break;
  269.             case 3:
  270.             case 18:
  271.             case 19:
  272.             case 20:
  273.             case 9:
  274.                 bounds[x].left=hgap*3;
  275.                 break;
  276.             case 4:
  277.             case 5:
  278.             case 6:
  279.             case 7:
  280.             case 8:
  281.                 bounds[x].left=hgap*4;
  282.                 break;
  283.         }
  284.         bounds[x].left+=thePtr->portRect.left;
  285.         bounds[x].top+=thePtr->portRect.top;
  286.         bounds[x].bottom=bounds[x].top+vgap;
  287.         bounds[x].right=bounds[x].left+hgap;
  288.     }
  289.     
  290.     for (x=2; x<vgap; x+=2)
  291.     {        
  292.         StartTiming();
  293.         for (y=0; y<25; y++)
  294.         {
  295.             if (y%2)   /* these scroll up */
  296.             {
  297.                 scrollsource=bounds[y];
  298.                 scrollsource.top+=2;
  299.                 scrolldest=scrollsource;
  300.                 OffsetRect(&scrolldest,0,-2);
  301.                 
  302.                 dest=bounds[y];
  303.                 dest.top=dest.bottom-2;
  304.                 
  305.                 CopyBits(&(thePtr->portBits), &(thePtr->portBits),
  306.                         &scrollsource, &scrolldest, 0, 0L);
  307.                 FillRect(&dest, black);
  308.             }
  309.             else    /* these scroll down */
  310.             {
  311.                 scrollsource=bounds[y];
  312.                 scrollsource.bottom-=2;
  313.                 scrolldest = scrollsource;
  314.                 OffsetRect(&scrolldest, 0, 2);
  315.                 
  316.                 dest=bounds[y];
  317.                 dest.bottom=dest.top+2;
  318.                 
  319.                 CopyBits(&(thePtr->portBits), &(thePtr->portBits),
  320.                         &scrollsource, &scrolldest, 0, 0L);
  321.                 
  322.                 FillRect(&dest, black);
  323.                 
  324.             }
  325.         }
  326.         TimeCorrection(3);
  327.     }
  328.     
  329.     SetRect(&dest, thePtr->portRect.left,
  330.         thePtr->portRect.top, thePtr->portRect.right,
  331.         thePtr->portRect.bottom);
  332.     FillRect(&dest, black);
  333. }
  334.  
  335. void CrashMrDo(GrafPtr thePtr)
  336. {
  337.     int            x, y;
  338.     int            vgap,hgap;
  339.     Rect        theRect, dest;
  340.     Rect        scrollsource, scrolldest;
  341.     Rect        bounds[25];
  342.     Boolean        everyOther;
  343.     int            width,height;
  344.     
  345.     SetPort(thePtr);
  346.     
  347.     width=thePtr->portRect.right-thePtr->portRect.left;
  348.     height=thePtr->portRect.bottom-thePtr->portRect.top;
  349.     
  350.     vgap=height/5;
  351.     hgap=width/5;
  352.     
  353.     for (x=0; x<25; x++)
  354.     {
  355.         switch (x)
  356.         {
  357.             case 0:
  358.             case 1:
  359.             case 2:
  360.             case 3:
  361.             case 4:
  362.                 bounds[x].top=0;
  363.                 break;
  364.             case 15:
  365.             case 16:
  366.             case 17:
  367.             case 18:
  368.             case 5:
  369.                 bounds[x].top=vgap;
  370.                 break;
  371.             case 14:
  372.             case 23:
  373.             case 24:
  374.             case 19:
  375.             case 6:
  376.                 bounds[x].top=vgap*2;
  377.                 break;
  378.             case 13:
  379.             case 22:
  380.             case 21:
  381.             case 20:
  382.             case 7:
  383.                 bounds[x].top=vgap*3;
  384.                 break;
  385.             case 12:
  386.             case 11:
  387.             case 10:
  388.             case 9:
  389.             case 8:
  390.                 bounds[x].top=vgap*4;
  391.                 break;
  392.         }
  393.         switch (x)
  394.         {
  395.             case 0:
  396.             case 15:
  397.             case 14:
  398.             case 13:
  399.             case 12:
  400.                 bounds[x].left=0;
  401.                 break;
  402.             case 1:
  403.             case 16:
  404.             case 23:
  405.             case 22:
  406.             case 11:
  407.                 bounds[x].left=hgap;
  408.                 break;
  409.             case 2:
  410.             case 17:
  411.             case 24:
  412.             case 21:
  413.             case 10:
  414.                 bounds[x].left=hgap*2;
  415.                 break;
  416.             case 3:
  417.             case 18:
  418.             case 19:
  419.             case 20:
  420.             case 9:
  421.                 bounds[x].left=hgap*3;
  422.                 break;
  423.             case 4:
  424.             case 5:
  425.             case 6:
  426.             case 7:
  427.             case 8:
  428.                 bounds[x].left=hgap*4;
  429.                 break;
  430.         }
  431.         bounds[x].left+=thePtr->portRect.left;
  432.         bounds[x].top+=thePtr->portRect.top;
  433.         bounds[x].bottom=bounds[x].top+vgap;
  434.         bounds[x].right=bounds[x].left+hgap;
  435.     }
  436.     
  437.     for (y=0; y<25; y++)
  438.     {        
  439.         if (y%2)   /* these scroll up */
  440.         {
  441.             scrollsource=bounds[y];
  442.             scrollsource.top+=4;
  443.             scrolldest=scrollsource;
  444.             OffsetRect(&scrolldest,0,-4);
  445.             
  446.             dest=bounds[y];
  447.             dest.top=dest.bottom-4;
  448.             
  449.             for (x=bounds[y].bottom-bounds[y].top-4; x>0; x-=4)
  450.             {
  451.                 StartTiming();
  452.                 CopyBits(&(thePtr->portBits), &(thePtr->portBits),
  453.                         &scrollsource, &scrolldest, 0, 0L);
  454.                 FillRect(&dest, black);
  455.  
  456.                 if (everyOther)
  457.                     TimeCorrection(1);
  458.                 everyOther=!everyOther;
  459.             }
  460.         }
  461.         else    /* these scroll down */
  462.         {
  463.             scrollsource=bounds[y];
  464.             scrollsource.bottom-=4;
  465.             scrolldest = scrollsource;
  466.             OffsetRect(&scrolldest, 0, 4);
  467.             
  468.             dest=bounds[y];
  469.             dest.bottom=dest.top+4;
  470.             
  471.             for(x = bounds[y].bottom-bounds[y].top-4; x > 0; x -= 4)
  472.             {
  473.                 StartTiming();
  474.                 CopyBits(&(thePtr->portBits), &(thePtr->portBits),
  475.                         &scrollsource, &scrolldest, 0, 0L);
  476.                 FillRect(&dest, black);
  477.  
  478.                 if (everyOther)
  479.                     TimeCorrection(1);
  480.                 everyOther=!everyOther;
  481.             }
  482.         }
  483.  
  484.         FillRect(&bounds[y], black);
  485.     }
  486. }
  487.  
  488. void CrashSpiral(GrafPtr thePtr)
  489. {
  490.     int            stop,sbottom,sleft,sright,iterrow,itercol,direction;
  491.     Rect        source;
  492.     Boolean        everyOther;
  493.     int            width,height;
  494.  
  495.     SetPort(thePtr);
  496.     
  497.     width=thePtr->portRect.right-thePtr->portRect.left;
  498.     height=thePtr->portRect.bottom-thePtr->portRect.top;
  499.     
  500.     everyOther=FALSE;
  501.     stop=0;
  502.     sbottom=height/20-(height%20 ? 0 : 1);
  503.     sleft=0;
  504.     sright=width/20-(width%20 ? 0 : 1);
  505.     direction=3;
  506.     iterrow=stop;
  507.     itercol=sleft;
  508.     while ((stop<=sbottom)&&(sleft<=sright))
  509.     {
  510.         StartTiming();
  511.         source.top=iterrow*20;
  512.         source.bottom=source.top+20;
  513.         source.left=itercol*20;
  514.         source.right=source.left+20;
  515.  
  516.         FillRect(&source, black);
  517.         
  518.         switch (direction)
  519.         {
  520.             case 0:  /* facing right */
  521.                 if (itercol==sright)
  522.                 {
  523.                     sbottom--;
  524.                     direction++;
  525.                     iterrow--;
  526.                 }
  527.                 else itercol++;
  528.                 break;
  529.             case 1:  /* facing up */
  530.                 if (iterrow==stop)   /* that reads "s top," not "stop" */
  531.                 {
  532.                     sright--;
  533.                     direction++;
  534.                     itercol--;
  535.                 }
  536.                 else iterrow--;
  537.                 break;
  538.             case 2:  /* facing left */
  539.                 if (itercol==sleft)
  540.                 {
  541.                     stop++;
  542.                     direction++;
  543.                     iterrow++;
  544.                 }
  545.                 else itercol--;
  546.                 break;
  547.             case 3:  /* facing down */
  548.                 if (iterrow==sbottom)
  549.                 {
  550.                     sleft++;
  551.                     direction=0;
  552.                     itercol++;
  553.                 }
  554.                 else iterrow++;
  555.                 break;
  556.         }
  557.         if (everyOther)
  558.             TimeCorrection(1);
  559.         everyOther=!everyOther;
  560.     }
  561. }
  562.